app.controller("familyTreeCtrl", ['$scope', '$modal', '$rootScope','FileUploader', 'editableOptions', 'editableThemes', 'lodash', function ($scope, $modal, $rootScope, FileUploader, editableOptions, editableThemes, lodash){
	$scope.info={};
	$scope.info.test="";
	$scope.info.title="测试"
	$scope.info.clicktest={}
	init()
	function init() {
		  var $ = go.GraphObject.make;
		  myDiagram =
	      $(go.Diagram, "myDiagramDiv",  // must be the ID or reference to div
	        {
	          "toolManager.hoverDelay": 100,  // 100 milliseconds instead of the default 850
	          allowCopy: false,
	          // initialContentAlignment: go.Spot.MiddleTop, // center the content
	          layout:  // create a TreeLayout for the family tree
	            $(go.TreeLayout,
	              { angle: 90, nodeSpacing: 50, layerSpacing: 40, layerStyle: go.TreeLayout.LayerUniform }),
	        });
	      //点击事件
	     myDiagram.addDiagramListener("ObjectSingleClicked", function (e) {
                console.log(e.subject.part.data);
                $scope.info.clicktest=e.subject.part.data;
          });
	    var bluegrad = '#90CAF9';
	    var pinkgrad = '#F48FB1';
	    var test1 = '#56e7ad';
	    myDiagram.add(
	      $(go.Part, "Table",
	        { position: new go.Point(-100, -10), selectable: false },
	        $(go.TextBlock, "Key",
	          { row: 0, font: "700 14px Droid Serif, sans-serif" }),  // end row 0
	        $(go.Panel, "Horizontal",
	          { row: 1, alignment: go.Spot.Left },
	          $(go.Shape, "Rectangle",
	            { desiredSize: new go.Size(30, 30), fill: bluegrad, margin: 5 }),
	          $(go.TextBlock, "Males",
	            { font: "700 13px Droid Serif, sans-serif" })
	        ),  // end row 1
	        $(go.Panel, "Horizontal",
	          { row: 2, alignment: go.Spot.Left },
	          $(go.Shape, "Rectangle",
	            { desiredSize: new go.Size(30, 30), fill: pinkgrad, margin: 5 }),
	          $(go.TextBlock, "Females",
	            { font: "700 13px Droid Serif, sans-serif" })
	        ),  // end row 2
	        $(go.Panel, "Horizontal",
	          { row: 3, alignment: go.Spot.Left },
	          $(go.Shape, "Rectangle",
	            { desiredSize: new go.Size(30, 30), fill: test1, margin: 5 }),
	          $(go.TextBlock, "test1",
	            { font: "700 13px Droid Serif, sans-serif" })
	        )  // end row 2
	      ));
	    // get tooltip text from the object's data
	    function tooltipTextConverter(person) {
	      var str = "";
	      str += "Born: " + person.birthYear;
	      if (person.deathYear !== undefined) str += "\nDied: " + person.deathYear;
	      if (person.reign !== undefined) str += "\nReign: " + person.reign;
	      return str;
	    }

	     // define tooltips for nodes
	    var tooltiptemplate =
	      $(go.Adornment, "Auto",
	        $(go.Shape, "Rectangle",
	          { fill: "whitesmoke", stroke: "black" }),
	        $(go.TextBlock,
	          { font: "bold 8pt Helvetica, bold Arial, sans-serif",
	            wrap: go.TextBlock.WrapFit,
	            margin: 5 },
	          new go.Binding("text", "", tooltipTextConverter))
	      );

	    // define Converters to be used for Bindings
	    function genderBrushConverter(gender) {
	      if (gender === "M") return bluegrad;
	      if (gender === "F") return pinkgrad;
	      if (gender === "T") return test1;
	      return "orange";
	    }

	    // replace the default Node template in the nodeTemplateMap
	    myDiagram.nodeTemplate =
	      $(go.Node, "Auto",
	        { deletable: false, toolTip: tooltiptemplate },
	        new go.Binding("text", "name"),
	        $(go.Shape, "Rectangle",
	          { fill: "lightgray",
	            stroke: null, strokeWidth: 0,
	            stretch: go.GraphObject.Fill,
	            alignment: go.Spot.Center },
	          new go.Binding("fill", "gender", genderBrushConverter)),
	        $(go.TextBlock,
	          { font: "700 12px Droid Serif, sans-serif",
	            textAlign: "center",
	            margin: 10, maxSize: new go.Size(80, NaN) },
	          new go.Binding("text", "name"))
	      );

	    // define the Link template
	    myDiagram.linkTemplate =
	      $(go.Link,  // the whole link panel
	        { routing: go.Link.Orthogonal, corner: 5, selectable: false },
	        $(go.Shape, { strokeWidth: 3, stroke: '#424242' }));  // the gray link shape

	    // here's the family data
	    var nodeDataArray = [
							    {
							        "key": 0,
							        "name": "Tom",
							        "gender": "M",
							        "birthYear": "1865",
							        "deathYear": "1936",
							        "reign": "1910-1936"
							    },
							    {
							        "key": 1,
							        "parent": 0,
							        "name": "订单",
							        "gender": "M",
							        "birthYear": "1894",
							        "deathYear": "1972",
							        "reign": "1936"
							    },
							    {
							        "key": 2,
							        "parent": 0,
							        "name": "智运",
							        "gender": "M",
							        "birthYear": "1895",
							        "deathYear": "1952",
							        "reign": "1936-1952"
							    },
							    {
							        "key": 3,
							        "parent": 0,
							        "name": "云仓",
							        "gender": "F",
							        "birthYear": "1895",
							        "deathYear": "1952",
							        "reign": "1936-1952"
							    },
							    {
							        "key": 4,
							        "parent": 0,
							        "name": "产品",
							        "gender": "M",
							        "birthYear": "1895",
							        "deathYear": "1952",
							        "reign": "1936-1952"
							    },
							    {
							        "key": 5,
							        "parent": 3,
							        "name": "产品管理",
							        "gender": "M",
							        "birthYear": "1895",
							        "deathYear": "1952",
							        "reign": "1936-1952"
							    },
							    {
							        "key": 6,
							        "parent": 5,
							        "name": "产品",
							        "gender": "M",
							        "birthYear": "1895",
							        "deathYear": "1952",
							        "reign": "1936-1952"
							    },
							    {
							        "key": 7,
							        "parent": 5,
							        "name": "产品架构",
							        "gender": "M",
							        "birthYear": "1895",
							        "deathYear": "1952",
							        "reign": "1936-1952"
							    },
							    {
							        "key": 8,
							        "parent": 6,
							        "name": "列表页",
							        "gender": "T",
							        "birthYear": "1895",
							        "deathYear": "1952",
							        "reign": "1936-1952"
							    },
							    {
							        "key": 9,
							        "parent": 6,
							        "name": "产品详细",
							        "gender": "M",
							        "birthYear": "1895",
							        "deathYear": "1952",
							        "reign": "1936-1952"
							    },
							    {
							        "key": 10,
							        "parent": 8,
							        "name": "查询列表",
							        "gender": "M",
							        "birthYear": "1895",
							        "deathYear": "1952",
							        "reign": "1936-1952"
							    },
							    {
							        "key": 11,
							        "parent": 8,
							        "name": "查询产品组",
							        "gender": "M",
							        "birthYear": "1895",
							        "deathYear": "1952",
							        "reign": "1936-1952"
							    },
							    {
							        "key": 12,
							        "parent": 10,
							        "name": "接口A",
							        "gender": "M",
							        "birthYear": "1895",
							        "deathYear": "1952",
							        "reign": "1936-1952"
							    },
							    {
							        "key": 13,
							        "parent": 11,
							        "name": "接口B",
							        "gender": "M",
							        "birthYear": "1895",
							        "deathYear": "1952",
							        "reign": "1936-1952"
							    },
							    {
							        "key": 14,
							        "parent": 11,
							        "name": "接口C",
							        "gender": "M",
							        "birthYear": "1895",
							        "deathYear": "1952",
							        "reign": "1936-1952"
							    }
							];

	    // create the model for the family tree
	    myDiagram.model = new go.TreeModel(nodeDataArray);

	    document.getElementById('zoomToFit').addEventListener('click', function() {
	      myDiagram.zoomToFit();
	    });

	    document.getElementById('centerRoot').addEventListener('click', function() {
	      myDiagram.scale = 1;
	      myDiagram.scrollToRect(myDiagram.findNodeForKey(0).actualBounds);
	    });
	    $scope.test=function() {  // called by button
		    var input = $scope.info.test;
		    if (!input) return;

		    myDiagram.startTransaction("highlight search");

		  if (input) {
		      // search four different data properties for the string, any of which may match for success
		      // create a case insensitive RegExp from what the user typed
		      var regex = new RegExp(input, "i");
		      var results = myDiagram.findNodesByExample({ name: regex },
		                                                 { nation: regex },
		                                                 { title: regex },
		                                                 { headOf: regex });
		      myDiagram.highlightCollection(results);
		      // try to center the diagram at the first node that was found
		      if (results.count > 0) myDiagram.centerRect(results.first().actualBounds);
		    } else {  // empty string only clears highlighteds collection
		      myDiagram.clearHighlighteds();
		    }

		    myDiagram.commitTransaction("highlight search");
		  }
	}	

}])
